package com.starter.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starter.home.dao.ServeInfoDao;
import com.starter.home.domain.entity.ServeInfo;
import com.starter.home.domain.entity.ServeOrder;
import com.starter.home.domain.entity.ServeType;
import com.starter.home.domain.vo.ServeInfoVO;
import com.starter.home.domain.vo.ServeOrderVO;
import com.starter.home.service.IServeImgService;
import com.starter.home.service.IServeInfoService;
import com.starter.home.service.IServeOrderService;
import com.starter.home.service.IServeTypeService;
import com.starter.home.util.CommonParam;
import com.starter.home.util.PageUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2020-05-01
 */
@Service
public class ServeInfoServiceImpl extends ServiceImpl<ServeInfoDao, ServeInfo> implements IServeInfoService {

    @Autowired
    private IServeTypeService serveTypeService;

    @Autowired
    private IServeOrderService orderService;

    @Autowired
    private IServeImgService imgService;

    @Override
    public PageUtil getInfoList(CommonParam commonParam) {
        QueryWrapper<ServeInfo> queryWrapper = initQuery(commonParam);
        if (commonParam.getSize() == 0){
            //获取全部的数据
            List<ServeInfo> list = this.baseMapper.selectList(queryWrapper);
            List<ServeInfoVO> listnew = CastServeInfoVO(list);
            return new PageUtil(list.size(),0,listnew);
        }
        //分页查询货物详情
        Page<ServeInfo> page = new Page<>(commonParam.getPage(),commonParam.getSize());
        queryWrapper.orderByAsc("id");
        Page<ServeInfo> pageData = this.baseMapper.selectPage(page, queryWrapper);
        List<ServeInfoVO> listnew = CastServeInfoVO(pageData.getRecords());
        return new PageUtil(pageData.getTotal(),pageData.getCurrent(),listnew);
    }

    @Override
    public void deleteById(Integer id) {
        this.baseMapper.deleteById(id);
    }

    @Override
    public List<ServeInfo> getByUserId(Integer userId) {
        QueryWrapper<ServeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public ServeInfoVO getInfoDetail(Integer infoId) {
        ServeInfoVO serveInfoVO = new ServeInfoVO();
        ServeInfo serveInfo = this.baseMapper.selectById(infoId);
        BeanUtils.copyProperties(serveInfo,serveInfoVO);
        ServeType byId = serveTypeService.findById(serveInfo.getCtype());
        serveInfoVO.setTypeName(Objects.isNull(byId) ? "未知" : byId.getTypeName());
        serveInfoVO.setHeathName(getHeathName(serveInfo.getHeathType()));
        serveInfoVO.setList(imgService.selectByInfoId(serveInfo.getId()));
        return serveInfoVO;
    }

    @Override
    public PageUtil getInfo(CommonParam params, Integer id, Integer i) {
        Page<ServeOrder> page = new Page<>(params.getPage(),params.getSize());
        QueryWrapper<ServeOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        if (i == null){
            queryWrapper.in("status",Arrays.asList(1,2,3,4));
        }else{
            queryWrapper.eq("status",i);
        }
        Page<ServeOrder> pageData = orderService.getBaseMapper().selectPage(page,queryWrapper);
        List<ServeOrder> records = pageData.getRecords();
        List<ServeOrderVO> list = orderService.converServeOrderVO(records);
        return new PageUtil(pageData.getTotal(),pageData.getCurrent(),list);
    }


    private List<ServeInfoVO> CastServeInfoVO(List<ServeInfo> queryWrapper) {
        List<ServeInfoVO> list = new ArrayList<>();
        ServeInfoVO serveInfoVO;
        for (ServeInfo serveInfo : queryWrapper) {
            serveInfoVO = new ServeInfoVO();
            BeanUtils.copyProperties(serveInfo,serveInfoVO);
            ServeType byId = serveTypeService.findById(serveInfo.getCtype());
            serveInfoVO.setTypeName(Objects.isNull(byId) ? "未知" : byId.getTypeName());
            serveInfoVO.setHeathName(getHeathName(serveInfo.getHeathType()));
            serveInfoVO.setList(imgService.selectByInfoId(serveInfo.getId()));
            list.add(serveInfoVO);
        }
        return list;
    }

    private String getHeathName(Integer heathType) {
        String heathName = null;
        switch (heathType){
            case 1 :
                heathName = "健康";
                break;
            case 2:
                heathName = "良好";
                break;
            case 3:
                heathName = "一般";
                break;
            default:
                heathName = "未知";
        }
        return heathName;
    }

    private QueryWrapper<ServeInfo> initQuery(CommonParam commonParam) {
        QueryWrapper<ServeInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(commonParam.getUserName())){
            queryWrapper.like("user_name",commonParam.getUserName());
        }
        if (StringUtils.isNotBlank(commonParam.getTitle())){
            queryWrapper.like("title",commonParam.getTitle());
        }
        if (StringUtils.isNotBlank(commonParam.getSearchValue())){
            queryWrapper.like("title",commonParam.getSearchValue());
        }
        if (ObjectUtils.isNotEmpty(commonParam.getType())){
            queryWrapper.eq("ctype",commonParam.getType());
        }
        if (ObjectUtils.isNotEmpty(commonParam.getCategoryId())){
            queryWrapper.eq("ctype",commonParam.getCategoryId());
        }
        if (ObjectUtils.isNotEmpty(commonParam.getUserId())){
            queryWrapper.eq("user_id",commonParam.getUserId());
        }
        return queryWrapper;
    }
}
